
#line 1 "USER\musical_scal.c" /0
  
#line 1 "USER\musical_scal.h" /0
 
 
  
#line 1 "USER\User_Def.h" /0









 
 
 
 
 
  
#line 1 "USER\MC51F003A4_COMMON.h" /0









 
 
 
 
 
  
#line 1 "C:\Program Files\Keil\SinoMCU_S1\Include\MC51F003A4.h" /0







 
 
 
 
 
 
 sfr PSW         = 0xD0;
 sfr DPS         = 0xD8;
 sfr ACC         = 0xE0;
 sfr B           = 0xF0;
 sfr AUXC        = 0xF1;
 sfr SP          = 0x81;
 sfr DPL         = 0x82;
 sfr DPH         = 0x83;
 sfr DPL1        = 0x84;
 sfr DPH1        = 0x85;
 sfr16 DPTR      = 0x82;
 sfr16 DPTR1     = 0x84;
 
 
 sfr PWRCR       = 0xF8;
 sfr OSCM        = 0xC0;
 sfr PCON        = 0x87;      
 sfr SLPCR       = 0x86;
 
 
 sfr RSTFLAG     = 0xE8;
 sfr WDTCR       = 0xCF;
 
 
 sfr P0          = 0x80;
 sfr P0MOD0      = 0xC1;
 sfr P0PU        = 0xC3;
 sfr P1          = 0x90;
 sfr P1MOD0      = 0x91;
 sfr P1MOD1      = 0x92;
 sfr P1PU        = 0x93;
 sfr P2          = 0xA0;
 sfr P2MOD0      = 0xA1;
 sfr P2MOD1      = 0xA2;
 sfr P2PU        = 0xA3;
 
 
 sfr TCON        = 0x88;
 sfr TMOD        = 0x89;
 sfr TL0         = 0x8A;
 sfr TL1         = 0x8B;
 sfr TH0         = 0x8C;
 sfr TH1         = 0x8D;
 sfr TCON1       = 0x8E;
 
 sfr T2CON       = 0xC8;
 sfr T2MOD       = 0xC9;
 sfr RCAPL2      = 0xCA;
 sfr RCAPH2      = 0xCB;
 sfr TL2         = 0xCC;
 sfr TH2         = 0xCD;
 sfr T2CON1      = 0xCE;
 
 sfr T3CR        = 0x8F;
 sfr TL3         = 0x96;
 sfr TH3         = 0x97;
 
 
 sfr PWM0CR      = 0xDD;
 sfr PWM0P       = 0xDE;
 sfr PWM0D       = 0xDF;
 
 sfr PWM1PH      = 0xE1;  
 sfr PWM1PL      = 0xE2;  
 sfr PWM1CR3     = 0xE3;  
 sfr PWM1CR2     = 0xE4;  
 sfr PWM1CR1     = 0xE5;  
 sfr PWM1CR0     = 0xE6;  
 sfr PWM1IER     = 0xE7;  
 sfr PWM12DH     = 0xE9;  
 sfr PWM12DL     = 0xEA;  
 sfr PWM101F     = 0xEB;  
 sfr PWM11DH     = 0xEC;  
 sfr PWM11DL     = 0xED;  
 sfr PWM10DH     = 0xEE;  
 sfr PWM10DL     = 0xEF;  
 sfr PWM15DH     = 0xF1;  
 sfr PWM15DL     = 0xF2;  
 sfr PWM14DH     = 0xF3;  
 sfr PWM14DL     = 0xF4;  
 sfr PWM123F     = 0xF5;  
 sfr PWM13DH     = 0xF6;  
 sfr PWM13DL     = 0xF7;  
 sfr PWM145F     = 0xFF;  
 
 
 sfr ADCCR0      = 0xF9;
 sfr ADCCR1      = 0xFA;
 sfr ADRIOS0     = 0xFB;
 sfr ADRIOS1     = 0xFC;
 sfr ADRL        = 0xFD;
 sfr ADRH        = 0xFE;
 sfr OSADJCR     = 0xFF;
 
 
 sfr SCON        = 0x98;
 sfr SBUF        = 0x99;
 sfr SADDR       = 0x9A;
 sfr SADEN       = 0x9B;
 sfr SCON_1      = 0x9C;
 sfr SBUF_1      = 0x9D;
 
 
 sfr SPCON       = 0xAD;
 sfr SPSTA       = 0xAE;
 sfr SPDAT       = 0xAF;
 
 
 sfr IE          = 0xA8;
 sfr IE1         = 0xA9;
 sfr EXTIS1      = 0xAB;
 sfr EXTEN1      = 0xAB;  
 sfr EXTIS       = 0xAC;
 sfr EXTEN       = 0xAC;  
 sfr IP          = 0xB8;
 sfr IPH         = 0xB9;
 sfr IP1         = 0xBA;
 sfr IPH1        = 0xBB;
 
 
 sfr LCDCR0      = 0xD9;
 sfr LCDSW       = 0xDA;
 
 
 sfr CRCCR       = 0xC1;  
 sfr CRCL        = 0xC2;  
 sfr CRCH        = 0xC3;  
 sfr CRCSTL      = 0xC4;  
 sfr CRCSTH      = 0xC5;  
 sfr CRCEDL      = 0xC6;  
 sfr CRCEDH      = 0xC7;  
 
 
 sfr IAPCR       = 0xE9;
 sfr IAPADL      = 0xEA;
 sfr IAPADH      = 0xEB;
 sfr IAPDB       = 0xEC;
 sfr IAPMD       = 0xEE;
 sfr IAPKEY      = 0xEF;
 
 
 
 sbit P01D       = P0^1;
 sbit P00D       = P0^0;
 
 sbit TF1        = TCON^7;
 sbit TR1        = TCON^6;
 sbit TF0        = TCON^5;
 sbit TR0        = TCON^4;
 sbit ADCIF      = TCON^0;
 
 sbit P17D       = P1^7;
 sbit P16D       = P1^6;
 sbit P15D       = P1^5;
 sbit P14D       = P1^4;
 sbit P13D       = P1^3;
 sbit P12D       = P1^2;
 sbit P11D       = P1^1;
 sbit P10D       = P1^0;
 
 sbit SM0        = SCON^7;
 sbit FE         = SCON^7;
 sbit SM1        = SCON^6;
 sbit RXOV       = SCON^6;
 sbit SM2        = SCON^5;
 sbit TXCOL      = SCON^5;
 sbit REN        = SCON^4;
 sbit TB8        = SCON^3;
 sbit RB8        = SCON^2;
 sbit TI         = SCON^1;
 sbit RI         = SCON^0;
 
 sbit P27D       = P2^7;
 sbit P26D       = P2^6;
 sbit P25D       = P2^5;
 sbit P24D       = P2^4;
 sbit P23D       = P2^3;
 sbit P22D       = P2^2;
 sbit P21D       = P2^1;
 sbit P20D       = P2^0;
 
 sbit EA         = IE^7;
 sbit ADCIE      = IE^6;
 sbit SPIIE      = IE^5;
 sbit UART0IE    = IE^4;
 sbit T1IE       = IE^3;
 sbit EXT1IE     = IE^2;
 sbit T0IE       = IE^1;
 sbit EXT0IE     = IE^0;
 
 sbit ADCIPL     = IP^6;
 sbit SPIIPL     = IP^5;
 sbit UART0IPL   = IP^4;
 sbit T1IPL      = IP^3;
 sbit EXT1IPL    = IP^2;
 sbit T0IPL      = IP^1;
 sbit EXT0IPL    = IP^0;
 
 sbit XTSPD      = OSCM^7;
 sbit STBH       = OSCM^5;
 sbit STBXT      = OSCM^4;
 sbit CLKS       = OSCM^2;
 sbit LFEN       = OSCM^1;
 sbit HFEN       = OSCM^0;
 
 sbit TF2        = T2CON^7;
 sbit EXTF2      = T2CON^6;
 sbit RCLKU0     = T2CON^5;
 sbit TCLKU0     = T2CON^4;
 sbit EX2EN      = T2CON^3;
 sbit TR2        = T2CON^2;
 sbit CT2        = T2CON^1;
 sbit CPRL2      = T2CON^0;
 
 sbit CY         = PSW^7;
 sbit AC         = PSW^6;
 sbit F0         = PSW^5;
 sbit RS1        = PSW^4;
 sbit RS0        = PSW^3;
 sbit OV         = PSW^2;
 sbit F1         = PSW^1;
 sbit P          = PSW^0;
 
 sbit SFRSL0     = DPS^6;
 sbit DIV16      = DPS^3;
 sbit MUL16      = DPS^2;
 sbit SEL        = DPS^0;
 
 sbit PORF       = RSTFLAG^7;
 sbit LVRF       = RSTFLAG^6;
 sbit RSTF       = RSTFLAG^5;
 sbit WDTRF      = RSTFLAG^4;
 sbit SWRF       = RSTFLAG^3;
 sbit SWRST      = RSTFLAG^0;  
 
 
 sbit E2_CLKEN   = PWRCR^6;
 sbit PWM1_CLKEN = PWRCR^5;
 sbit PWM0_CLKEN = PWRCR^4;
 sbit SPI_CLKEN  = PWRCR^3;
 sbit ADC_CLKEN  = PWRCR^2;
 sbit LCD_CLKEN  = PWRCR^1;
 sbit OCD_CLKEN  = PWRCR^0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 




 
#line 15 "USER\MC51F003A4_COMMON.h" /0
 
  
#line 1 "C:\Program Files\Keil\C51\Inc\intrins.h" /0






 
 
 
 
 
 #pragma SAVE
 
 
#line 15 "C:\Program Files\Keil\C51\Inc\intrins.h" /1
 
 
 
#line 18 "C:\Program Files\Keil\C51\Inc\intrins.h" /0
 
 extern void          _nop_     (void);
 extern bit           _testbit_ (bit);
 extern unsigned char _cror_    (unsigned char, unsigned char);
 extern unsigned int  _iror_    (unsigned int,  unsigned char);
 extern unsigned long _lror_    (unsigned long, unsigned char);
 extern unsigned char _crol_    (unsigned char, unsigned char);
 extern unsigned int  _irol_    (unsigned int,  unsigned char);
 extern unsigned long _lrol_    (unsigned long, unsigned char);
 extern unsigned char _chkfloat_(float);
 
#line 29 "C:\Program Files\Keil\C51\Inc\intrins.h" /1
 
 
 
#line 32 "C:\Program Files\Keil\C51\Inc\intrins.h" /0
 
 extern void          _push_    (unsigned char _sfr);
 extern void          _pop_     (unsigned char _sfr);
 
 
 #pragma RESTORE
 
 
 
#line 16 "USER\MC51F003A4_COMMON.h" /0
 
 
 typedef enum {RESET = 0, SET = !RESET} flagstatus, ITstatus;
 
 
 
 
 
 typedef   signed	char			int8_t;
 typedef   signed	int				int16_t;
 typedef   signed	long 			int32_t;
 
 typedef unsigned	char 			uint8_t;
 typedef unsigned	int 			uint16_t;
 typedef unsigned	long			uint32_t;
 typedef unsigned	char			u8;
 typedef unsigned	int 			u16;
 typedef unsigned	long			u32;
 typedef unsigned	char			uchar;
 typedef unsigned	int 			uint;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#line 15 "USER\User_Def.h" /0
 
  
#line 1 ".\HARDWARE\GPIO\GPIO.h" /0









 
 
 
 
  
#line 1 "USER\User_Def.h" /0









 
 
 
#line 13 "USER\User_Def.h" /1
  
 
  
  
  
  
  
  
 
 



 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
 
 
  
 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
 
 
 
  
 
  
 
 
 
#line 124 "USER\User_Def.h" /0
 
 
 
#line 14 ".\HARDWARE\GPIO\GPIO.h" /0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 void GPIO_Init(void);
 
 
 
 
 
 
#line 16 "USER\User_Def.h" /0
 
  
#line 1 ".\HARDWARE\UART\UART.h" /0









 
 
 
  
#line 1 "USER\User_Def.h" /0









 
 
 
#line 13 "USER\User_Def.h" /1
  
 
  
  
  
  
  
  
 
 



 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
 
 
  
 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
 
 
 
  
 
  
 
 
 
#line 124 "USER\User_Def.h" /0
 
 
 
#line 13 ".\HARDWARE\UART\UART.h" /0
 
  
#line 1 "C:\Program Files\Keil\C51\Inc\stdio.h" /0






 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 typedef unsigned int size_t;
 
 
 #pragma SAVE
 #pragma REGPARMS
 extern char _getkey (void);
 extern char getchar (void);
 extern char ungetchar (char);
 extern char putchar (char);
 extern int printf   (const char *, ...);
 extern int sprintf  (char *, const char *, ...);
 extern int vprintf  (const char *, char *);
 extern int vsprintf (char *, const char *, char *);
 extern char *gets (char *, int n);
 extern int scanf (const char *, ...);
 extern int sscanf (char *, const char *, ...);
 extern int puts (const char *);
 
 #pragma RESTORE
 
 
 
#line 14 ".\HARDWARE\UART\UART.h" /0
 



 
 
 
 
 void UART1_Init(void);
 char putchar(char tx_data);
 
 
 
 
 
#line 17 "USER\User_Def.h" /0
 
  
#line 1 ".\HARDWARE\PWM0\PWM0.h" /0









 
 
 
 
 
  
#line 1 "USER\User_Def.h" /0









 
 
 
#line 13 "USER\User_Def.h" /1
  
 
  
  
  
  
  
  
 
 



 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
 
 
  
 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
 
 
 
  
 
  
 
 
 
#line 124 "USER\User_Def.h" /0
 
 
 
#line 15 ".\HARDWARE\PWM0\PWM0.h" /0
 
 
 void PWM0_User_Init(void);
 
 
 
 
#line 18 "USER\User_Def.h" /0
 
  
#line 1 "C:\Program Files\Keil\C51\Inc\stdio.h" /0






 
 
 
#line 10 "C:\Program Files\Keil\C51\Inc\stdio.h" /1
  
 
 
  
 
 
 
  
 
 
 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#line 43 "C:\Program Files\Keil\C51\Inc\stdio.h" /0
 
#line 19 "USER\User_Def.h" /0
 
  
#line 1 "C:\Program Files\Keil\C51\Inc\stdarg.h" /0







 
 
 
 
 
 
 
#line 15 "C:\Program Files\Keil\C51\Inc\stdarg.h" /1
  
 
#line 17 "C:\Program Files\Keil\C51\Inc\stdarg.h" /0
 
 
 
 typedef char *va_list;
 
 
 
 
#line 25 "C:\Program Files\Keil\C51\Inc\stdarg.h" /1
  
 
 
#line 28 "C:\Program Files\Keil\C51\Inc\stdarg.h" /0
 
 
 
 
 
 
 
#line 20 "USER\User_Def.h" /0
 
 
 



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 typedef union {
 unsigned char byte;
 struct
 {
 u8 bit0 : 1;
 u8 bit1 : 1;
 u8 bit2 : 1;
 u8 bit3 : 1;
 u8 bit4 : 1;
 u8 bit5 : 1;
 u8 bit6 : 1;
 u8 bit7 : 1;
 } bits;
 }bit_flag;
 extern volatile bit_flag flag1;
 extern volatile uint32_t millisecondTick;
 extern code uint16_t musicalScalFreq[21];
 
 
 void delay_ms(uint32_t ms);
 
 
 
 
 
#line 70 "USER\User_Def.h" /1
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
#line 92 "USER\User_Def.h" /0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 void mylog(const char *str, ...);
 
#line 118 "USER\User_Def.h" /1
  
 
#line 120 "USER\User_Def.h" /0
 
 
 
 
 
 
 
#line 3 "USER\musical_scal.h" /0
 
 typedef enum {
 
 DO_L = 0,
 RE_L,
 MI_L,
 FA_L,
 SO_L, 
 LA_L, 
 SI_L,
 
 DO_M, 
 RE_M,
 MI_M,
 FA_M, 
 SO_M,
 LA_M,
 SI_M, 
 
 DO_H,
 RE_H,
 MI_H,
 FA_H,
 SO_H,
 LA_H,
 SI_H,
 }musicalScale_e;
 typedef enum
 {
 RECORD_NULL = 0,
 RECORD_START,
 RECORD_STARTED,
 RECORDING,
 RECORD_STOP,
 RECORD_REPALY,
 } recordState_t;
 typedef enum {
 REPLAY_NULL = 0,
 REPLAY_START,
 REPLAY_PLAYING,
 REPLAY_STOP,
 REPLAY_NEXT,
 }replayState_t;
 extern recordState_t recordState;
 extern replayState_t replayState;
 void speak_output_freq(unsigned long freq);
 unsigned char keyScan();
 void speaker_ouput_musicalscal(musicalScale_e m);
 void speakerSetFreq(uint16_t freq);
 void speakerHandleTask();
 void recordStart();
 void recordStop();
 void recordRecord(uint16_t key);
 void musicRecordInit();
 void recordAndReplayTask();
 uint8_t isReplayNull();
 
#line 1 "USER\musical_scal.c" /0
 
  
#line 1 "USER\User_Def.h" /0









 
 
 
#line 13 "USER\User_Def.h" /1
  
 
  
  
  
  
  
  
 
 



 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
 
 
  
 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
 
 
 
  
 
  
 
 
 
#line 124 "USER\User_Def.h" /0
 
 
 
#line 2 "USER\musical_scal.c" /0
 
  
#line 1 ".\HARDWARE\PWM1\PWM.h" /0









 
 
 
 
  
#line 1 "USER\User_Def.h" /0









 
 
 
#line 13 "USER\User_Def.h" /1
  
 
  
  
  
  
  
  
 
 



 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
 
 
  
 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
 
 
 
  
 
  
 
 
 
#line 124 "USER\User_Def.h" /0
 
 
 
#line 14 ".\HARDWARE\PWM1\PWM.h" /0
 
 
 
 
 
 
 
 void PWM1_Init(void);
 void PWM1_SET_CHANNEL_10(uint16_t period, uint16_t pulse);
 void PWM1_SET_CHANNEL_10_PULSE( uint16_t pulse);
 void PWM1_OFF();
 void PWM1_SET_FREQ(uint16_t freq);
 void pwm1_set_freq_10(uint16_t freq);
 
 
 
 
#line 3 "USER\musical_scal.c" /0
 
  
#line 1 ".\HARDWARE\TIMER\TIMER.h" /0









 
 
 
  
#line 1 "USER\User_Def.h" /0









 
 
 
#line 13 "USER\User_Def.h" /1
  
 
  
  
  
  
  
  
 
 



 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
 
 
  
 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
 
 
 
  
 
  
 
 
 
#line 124 "USER\User_Def.h" /0
 
 
 
#line 13 ".\HARDWARE\TIMER\TIMER.h" /0
 
 
 
 
 
 
 
 void Timer0_Init(void);
 
 
 
 
#line 4 "USER\musical_scal.c" /0
 
  
#line 1 "C:\Program Files\Keil\C51\Inc\STDIO.H" /0






 
 
 
#line 10 "C:\Program Files\Keil\C51\Inc\STDIO.H" /1
  
 
 
  
 
 
 
  
 
 
 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#line 43 "C:\Program Files\Keil\C51\Inc\STDIO.H" /0
 
#line 5 "USER\musical_scal.c" /0
 
  
#line 1 ".\HARDWARE\EEPROM\EEPROM.h" /0









 
 
 
 
  
#line 1 "USER\User_Def.h" /0









 
 
 
#line 13 "USER\User_Def.h" /1
  
 
  
  
  
  
  
  
 
 



 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
 
 
  
 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  
  
 
 
 
 
 
 
  
 
  
 
 
 
#line 124 "USER\User_Def.h" /0
 
 
 
#line 14 ".\HARDWARE\EEPROM\EEPROM.h" /0
 
 
 void EEPROM_Chip_Erase();
 void EEPROM_Page_Erase(uint8_t Page);
 void EEPROM_Page_Write(uint8_t page, uint8_t *W_data);
 void EEPROM_Byte_Erase(uint16_t addar);
 void EEPROM_Byte_Write(uint16_t addar, uint8_t W_data);
 uint8_t EEPROM_Byte_Read(uint16_t addr);
 void EEPROM_Read(uint16_t addr, uint8_t xdata *R_data, uint16_t len);
 
 
 
 
#line 6 "USER\musical_scal.c" /0
 
  
#line 1 "C:\Program Files\Keil\C51\Inc\STRING.H" /0







 
 
 
 
 
 
#line 14 "C:\Program Files\Keil\C51\Inc\STRING.H" /1
  
 
 
#line 17 "C:\Program Files\Keil\C51\Inc\STRING.H" /0
 
 
#line 19 "C:\Program Files\Keil\C51\Inc\STRING.H" /1
  
 
#line 21 "C:\Program Files\Keil\C51\Inc\STRING.H" /0
 
 #pragma SAVE
 #pragma REGPARMS
 extern char  *strcat  (char *s1, const char *s2);
 extern char  *strncat (char *s1, const char *s2, size_t n);
 
 extern char   strcmp  (const char *s1, const char *s2);
 extern char   strncmp (const char *s1, const char *s2, size_t n);
 
 extern char  *strcpy  (char *s1, const char *s2);
 extern char  *strncpy (char *s1, const char *s2, size_t n);
 
 extern size_t strlen  (const char *);
 
 extern char  *strchr  (const char *s, char c);
 extern int    strpos  (const char *s, char c);
 extern char  *strrchr (const char *s, char c);
 extern int    strrpos (const char *s, char c);
 
 extern size_t strspn  (const char *s, const char *set);
 extern size_t strcspn (const char *s, const char *set);
 extern char  *strpbrk (const char *s, const char *set);
 extern char  *strrpbrk(const char *s, const char *set);
 extern char  *strstr  (const char *s, const char *sub);
 extern char  *strtok  (char *str, const char *set);
 
 extern char   memcmp  (const void *s1, const void *s2, size_t n);
 extern void  *memcpy  (void *s1, const void *s2, size_t n);
 extern void  *memchr  (const void *s, char val, size_t n);
 extern void  *memccpy (void *s1, const void *s2, char val, size_t n);
 extern void  *memmove (void *s1, const void *s2, size_t n);
 extern void  *memset  (void *s, char val, size_t n);
 #pragma RESTORE
 
 
#line 7 "USER\musical_scal.c" /0
 
  
#line 1 ".\HARDWARE\PWM1/PWM.h" /0









 
 
#line 12 ".\HARDWARE\PWM1/PWM.h" /1
  
 
  
 
  
 
 
 
 
 
 
 
 
 
 
 
#line 28 ".\HARDWARE\PWM1/PWM.h" /0
 
 
 
#line 8 "USER\musical_scal.c" /0
 
 
 recordState_t recordState;
 replayState_t replayState;
 
 typedef union {
 unsigned int word;
 struct
 {
 u8 bit0 : 1;
 u8 bit1 : 1;
 u8 bit2 : 1;
 u8 bit3 : 1;
 u8 bit4 : 1;
 u8 bit5 : 1;
 u8 bit6 : 1;
 u8 bit7 : 1;
 u8 bit8 : 1;
 u8 bit9 : 1;
 u8 bit10 : 1;
 u8 bit11 : 1;
 u8 bit12 : 1;
 u8 bit13 : 1;
 u8 bit14 : 1;
 u8 bit15 : 1;
 } bits;
 }key_flag_t;
 
 
 
 
 xdata key_flag_t ks;
 xdata uint32_t keyDnT[16];



 
 

















 
 uint8_t keyScan()
 {
 
 uint32_t keyTimeMax = 0;
 uint8_t keyMax = 0;
 uint8_t i = 0;
 
  if(P15D){ if(!ks.bits.bit1) { keyDnT[1]=millisecondTick; ks.bits.bit1=1; }}else { ks.bits.bit1 = 0; keyDnT[1]=0;};
 
  if(P16D){ if(!ks.bits.bit2) { keyDnT[2]=millisecondTick; ks.bits.bit2=1; }}else { ks.bits.bit2 = 0; keyDnT[2]=0;};
  if(P01D){ if(!ks.bits.bit3) { keyDnT[3]=millisecondTick; ks.bits.bit3=1; }}else { ks.bits.bit3 = 0; keyDnT[3]=0;};
  if(P20D){ if(!ks.bits.bit4) { keyDnT[4]=millisecondTick; ks.bits.bit4=1; }}else { ks.bits.bit4 = 0; keyDnT[4]=0;};
  if(P21D){ if(!ks.bits.bit5) { keyDnT[5]=millisecondTick; ks.bits.bit5=1; }}else { ks.bits.bit5 = 0; keyDnT[5]=0;};
  if(P22D){ if(!ks.bits.bit6) { keyDnT[6]=millisecondTick; ks.bits.bit6=1; }}else { ks.bits.bit6 = 0; keyDnT[6]=0;};
  if(P23D){ if(!ks.bits.bit7) { keyDnT[7]=millisecondTick; ks.bits.bit7=1; }}else { ks.bits.bit7 = 0; keyDnT[7]=0;};
  if(P24D){ if(!ks.bits.bit8) { keyDnT[8]=millisecondTick; ks.bits.bit8=1; }}else { ks.bits.bit8 = 0; keyDnT[8]=0;};
  if(P25D){ if(!ks.bits.bit9) { keyDnT[9]=millisecondTick; ks.bits.bit9=1; }}else { ks.bits.bit9 = 0; keyDnT[9]=0;};
  if(P26D){ if(!ks.bits.bit10) { keyDnT[10]=millisecondTick; ks.bits.bit10=1; }}else { ks.bits.bit10 = 0; keyDnT[10]=0;};
  if(P27D){ if(!ks.bits.bit11) { keyDnT[11]=millisecondTick; ks.bits.bit11=1; }}else { ks.bits.bit11 = 0; keyDnT[11]=0;};
  if(P10D){ if(!ks.bits.bit12) { keyDnT[12]=millisecondTick; ks.bits.bit12=1; }}else { ks.bits.bit12 = 0; keyDnT[12]=0;};
  if(P11D){ if(!ks.bits.bit13) { keyDnT[13]=millisecondTick; ks.bits.bit13=1; }}else { ks.bits.bit13 = 0; keyDnT[13]=0;};
  if(P12D){ if(!ks.bits.bit14) { keyDnT[14]=millisecondTick; ks.bits.bit14=1; }}else { ks.bits.bit14 = 0; keyDnT[14]=0;};
  if(P14D){ if(!ks.bits.bit15) { keyDnT[15]=millisecondTick; ks.bits.bit15=1; }}else { ks.bits.bit15 = 0; keyDnT[15]=0;};
 for (i = 1;i <= 15;i++)
 {
 if (keyDnT[i] >= keyTimeMax && keyDnT[i] != 0) 
 {
 keyTimeMax = keyDnT[i];
 keyMax = i;
 }
 
 }
 return keyMax;
 }
 
 static data uint16_t recordedKey = 0;
 static data uint16_t recordedKeyPre = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 





 
 xdata union {
 struct __packed {
 uint8_t Data; 
 uint16_t Time; 
 }v;
 uint8_t raw[3];
 }recordBuff, replayBuff, tmpBuff;
 





 
 uint16_t searchStartFlag(uint16_t from)
 {
 uint16_t i = 0;
  EA=0;
  IAPCR|=0x01;  
 if (from < 7U) from = 7U;
 for (i = from - (from - 7U) % 3;i < 1024;i += 3)
 {
 tmpBuff.raw[0] = *(uint8_t volatile code*)(i + 0);
 tmpBuff.raw[1] = *(uint8_t volatile code*)(i + 1);
 tmpBuff.raw[2] = *(uint8_t volatile code*)(i + 2);
 if ((((tmpBuff.v.Data)&0xC0)==0xC0))
 {
 break;
 }
 }
  IAPCR&=~0x01;  
  EA=1;
 return ((i <= 1023) ? i : 0); 
 }
 uint16_t getLastAddr()
 {
 EEPROM_Read(4U, tmpBuff.raw, 3);
 if ((((tmpBuff.v.Data)&0xA0)==0xA0))
 {
 return tmpBuff.v.Time;
 }
 return 7U;
 }
 xdata uint16_t replayStartAddr = 0;
 xdata uint16_t replayStopAddr = 0;
 xdata uint16_t lastAddr = 0;
 xdata uint16_t keyValueOutput = 0;
 int16_t initData = 0;
 void musicRecordInit()
 {
 
 initData = EEPROM_Byte_Read(0); 
 if ((((initData)&0xD0)==0xD0)) 
 {
 EEPROM_Read(4, tmpBuff.raw, 3);
 if ((((tmpBuff.v.Data)&0xA0)==0xA0))
 {
 lastAddr = tmpBuff.v.Time;
 }
 else
 {
 lastAddr = 7U;
 }
 replayStartAddr = searchStartFlag(7U);
 }
 else
 {
 EEPROM_Chip_Erase(); 
 lastAddr = 7U;
  mylog("EEPROM_Chip_Erase\n");
 }
 initData = sizeof(recordBuff);
  mylog("recordBuff %d\n", initData);
 }
 void musicRecordHandle()
 {
 static uint32_t startTime = 0;
 switch (recordState)
 {
 case RECORD_START:
 lastAddr = getLastAddr();
 recordState = RECORDING;
 startTime = millisecondTick;
 recordBuff.v.Data = 0xC0;
 break;
 case RECORD_STARTED:
 break;
 case RECORDING:
 if (recordedKey != recordedKeyPre) 
 {
 
 recordedKeyPre = recordedKey;
 recordBuff.v.Time = millisecondTick - startTime; 
 startTime = millisecondTick;
 if (lastAddr >= 1023) lastAddr = 7U; 
 EEPROM_Byte_Erase(lastAddr + 0);
 EEPROM_Byte_Erase(lastAddr + 1);
 EEPROM_Byte_Erase(lastAddr + 2);
 EEPROM_Byte_Write(lastAddr + 0, recordBuff.raw[0]);
 EEPROM_Byte_Write(lastAddr + 1, recordBuff.raw[1]);
 EEPROM_Byte_Write(lastAddr + 2, recordBuff.raw[2]);
  mylog("pD_%d:%d T:%u\n", lastAddr, (int)recordBuff.v.Data, recordBuff.v.Time);
 lastAddr += 3;
 recordBuff.v.Data = 0x90 | recordedKey;  
 }
 break;
 case RECORD_STOP:
 recordBuff.v.Time = millisecondTick - startTime; 
 if (lastAddr >= 1023) lastAddr = 7U; 
 
 
 EEPROM_Byte_Erase(lastAddr + 0);
 EEPROM_Byte_Erase(lastAddr + 1);
 EEPROM_Byte_Erase(lastAddr + 2);
 EEPROM_Byte_Write(lastAddr + 0, recordBuff.raw[0]);
 EEPROM_Byte_Write(lastAddr + 1, recordBuff.raw[1]);
 EEPROM_Byte_Write(lastAddr + 2, recordBuff.raw[2]);
  mylog("LD_%d:%d-%u\n", lastAddr, (int)recordBuff.v.Data, recordBuff.v.Time);
 lastAddr += 3;
 
 if (lastAddr >= 1023) lastAddr = 7U; 
 recordBuff.v.Data = 0x30;
 recordBuff.v.Time = lastAddr + 3;
 EEPROM_Byte_Erase(lastAddr + 0);
 EEPROM_Byte_Erase(lastAddr + 1);
 EEPROM_Byte_Erase(lastAddr + 2);
 EEPROM_Byte_Write(lastAddr + 0, recordBuff.raw[0]);
 EEPROM_Byte_Write(lastAddr + 1, recordBuff.raw[1]);
 EEPROM_Byte_Write(lastAddr + 2, recordBuff.raw[2]);
  mylog("LD_%d:%d-%u\n", lastAddr, (int)recordBuff.v.Data, recordBuff.v.Time);
 lastAddr += 3;
 
 
 if (lastAddr >= 1023) lastAddr = 7U; 
 recordBuff.v.Data = 0xA0;
 recordBuff.v.Time = lastAddr;
 EEPROM_Byte_Erase(4);
 EEPROM_Byte_Erase(5);
 EEPROM_Byte_Erase(6);
 EEPROM_Byte_Write(4, recordBuff.raw[0]);
 EEPROM_Byte_Write(5, recordBuff.raw[1]);
 EEPROM_Byte_Write(6, recordBuff.raw[2]);
  mylog("END_%d:%d-%u\n", lastAddr, (int)recordBuff.v.Data, recordBuff.v.Time);
 recordState = RECORD_NULL;
 
 if (!(((initData)&0xD0)==0xD0))
 {
 EEPROM_Byte_Erase(0);
 EEPROM_Byte_Write(0, 0xD0);
 }
 break;
 default:
 break;
 }
 
 }
 
 
 
 uint8_t isReplayNull()
 {
 return (replayState == REPLAY_NULL);
 }
 void recordStart()
 {
 recordState = RECORD_START;
 }
 void recordStop()
 {
 recordState = RECORD_STOP;
 }
 void recordRecord(uint16_t key)
 {
 recordedKey = key;
 }
 
 
 void replayHandle()
 {
 static xdata uint32_t startTime = 0;
 
 
 static xdata uint16_t replayAddr = 0;
 
 switch (replayState)
 {
 case REPLAY_START:
 if (replayStartAddr != 0)
 {
 EEPROM_Read(replayStartAddr, replayBuff.raw, 3);
 replayState = REPLAY_PLAYING;
 replayAddr = replayStartAddr;
 startTime = millisecondTick;
 }
 else
 {
 replayStartAddr = searchStartFlag(7U);
 if (replayStartAddr == 0)
 {
 replayState = REPLAY_NULL;
  mylog("REPLAY_NULL\n");
 }
 else
 {
  mylog("get replayStartAddr:%d\n", replayStartAddr);
 EEPROM_Read(replayStartAddr, replayBuff.raw, 3);                                
  mylog("SD:%d T:%u\n", (int)replayBuff.v.Data, replayBuff.v.Time);
 replayState = REPLAY_PLAYING;
 replayAddr = replayStartAddr + 3;
 startTime = millisecondTick;
 }
 }
 replayBuff.v.Time = 500; 
 break;
 case REPLAY_PLAYING:
 if (millisecondTick > (startTime + replayBuff.v.Time))
 {
 startTime = millisecondTick;
 EEPROM_Read(replayAddr, replayBuff.raw, 3); 
  mylog("pD_%d: %d T:%u\t", replayAddr, (int)replayBuff.v.Data, replayBuff.v.Time);
 
 
 if ((((replayBuff.v.Data)&0x90)==0x90))
 {



 
 keyValueOutput = ((replayBuff.v.Data)&0x0F);
 if (keyValueOutput)
 {
 pwm1_set_freq_10(musicalScalFreq[keyValueOutput - 1 + SO_L]);
 }
 else
 {
 PWM1_OFF();
 }
  mylog("keyValueOutput:%d\n", keyValueOutput);
 }
 if ((((replayBuff.v.Data)&0x30)==0x30))  
 {
  mylog("meet stop\n");
 replayState = REPLAY_STOP;
 }
 if ((((replayBuff.v.Data)&0xC0)==0xC0))
 {
 
 if (replayAddr != replayStartAddr) 
 {
  mylog("meet another start\n");
 replayState = REPLAY_STOP;
 }
 }
 replayAddr += 3;
 if (replayAddr > 1023) replayAddr = 7U; 
 
 }
 break;
 case REPLAY_STOP:



 
 replayStartAddr = searchStartFlag(replayAddr);
 PWM1_OFF();
 replayState = REPLAY_NULL;
  mylog("REPLAY_STOP1:%d\n", replayStartAddr);
 break;
 default:
 break;
 }
 }
 void recordAndReplayTask()
 {
 static xdata uint32_t tRecord = 0;
 static xdata uint32_t tRecord2 = 0;
 static xdata uint32_t rt = 0;
 static xdata uint8_t pressTimesRecord;
 if (millisecondTick > rt + 20)
 {
 rt = millisecondTick;
 if (P17D)
 
 {
 if (!tRecord)
 tRecord = millisecondTick;
 if (!tRecord2)
 tRecord2 = millisecondTick;
 pressTimesRecord |= 0x80;  
 }
 else
 {
 
 if (tRecord2)
 {
 if (millisecondTick > (tRecord2 + 1000))
 {
 tRecord2 = 0;
 pressTimesRecord |= 0x40;  
 }
 }
 
 if (pressTimesRecord & 0x80)
 {
 
 pressTimesRecord++;
 pressTimesRecord &= 0x7F;  
 }
 tRecord = 0;
 }
 if (tRecord)
 {
 if (millisecondTick > (tRecord + 3000)) 
 {
  mylog("long times\n");
 pressTimesRecord = 0;
 pwm1_set_freq_10(musicalScalFreq[DO_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[RE_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[MI_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[FA_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[SO_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[LA_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[SI_H]);
 delay_ms(100);
 PWM1_OFF();
 EEPROM_Chip_Erase();
 PWM1_OFF();
 replayStartAddr = 0;
 recordState = RECORD_NULL;
 replayState = REPLAY_NULL;
 tRecord = 0;
 }
 
 }
 }
 if (pressTimesRecord & 0x40)
 {
 
 if ((pressTimesRecord & 0x3f) == 1)  
 {
  mylog("1 times\n");
 if (recordState == RECORDING)
 {
 recordState = RECORD_STOP;
 pwm1_set_freq_10(musicalScalFreq[SI_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[LA_H]);
 delay_ms(100);
 PWM1_OFF();
 
  mylog("RECORD_STOP\n");
 }
 else if (replayState == REPLAY_NULL)
 {
 replayState = REPLAY_START;
 pwm1_set_freq_10(musicalScalFreq[SI_H]);
 delay_ms(100);
 PWM1_OFF();
  mylog("REPLAY_START\n");
 }
 else if (replayState == REPLAY_PLAYING)
 {
 replayState = REPLAY_STOP;
  mylog("REPLAY_STOP\n");
 }
 }
 if ((pressTimesRecord & 0x3f) == 2)  
 {
  mylog("2 times\n");
 if (recordState != RECORDING)
 {
 recordState = RECORD_START;
 
 PWM1_OFF();
 
 pwm1_set_freq_10(musicalScalFreq[SO_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[LA_H]);
 delay_ms(100);
 pwm1_set_freq_10(musicalScalFreq[SI_H]);
 delay_ms(100);
 PWM1_OFF();
 
  mylog("RECORD_START\n");
 
 }
 
 
 }
 pressTimesRecord = 0;
 }
 if (recordState != RECORD_NULL)
 musicRecordHandle();
 if (replayState != REPLAY_NULL)
 replayHandle();
 }
